import 'package:flutter/material.dart';
import 'package:flutter_layout/pages/custom/received_message_screen.dart';
import 'package:flutter_layout/pages/custom/send_message_screen.dart';

class ListViewPage extends StatefulWidget {
  const ListViewPage({Key? key}) : super(key: key);

  @override
  _ListViewPageState createState() => _ListViewPageState();
}

class _ListViewPageState extends State<ListViewPage> {
  var listData = <String>[];

  @override
  Widget build(BuildContext context) {
    listData.clear();
    listData
      ..add("Hello")
      ..add("Hi, how are you")
      ..add("I am great how are you doing")
      ..add("I am also fine")
      ..add("Can we meet tomorrow?")
      ..add("Yes, of course we will meet tomorrow");
    return Scaffold(
        appBar: AppBar(
          title: const Text('custom'),
        ),
        body:
             separated()
            // custom()
        // builder()
        // decoration: const BoxDecoration(image: DecorationImage(image: AssetImage("assets/images/common/group_add.png"), fit: BoxFit.cover)),
        );
  }

  ListView builder() {
    return ListView.builder(
        physics: const BouncingScrollPhysics(),
        itemCount: listData.length * 100,
        itemBuilder: (context, index) {
          if (index % 2 == 0) {
            return SendMessageScreen(message: listData[index % listData.length]);
          }
          return ReceivedMessageScreen(message: listData[index % listData.length]);
        });
  }

  ListView custom() {
    return ListView.custom(
        physics: const BouncingScrollPhysics(),
        childrenDelegate: SliverChildBuilderDelegate((context, index) {
          if (index % 2 == 0) {
            return SendMessageScreen(message: listData[index % listData.length]);
          }
          return ReceivedMessageScreen(message: listData[index % listData.length]);
        }, childCount: listData.length * 100));
  }

  ListView separated() {
    return ListView.separated(
        physics: const BouncingScrollPhysics(),
        itemBuilder: (context, index) {
          if (index % 2 == 0) {
            return SendMessageScreen(message: listData[index % listData.length]);
          }
          return ReceivedMessageScreen(message: listData[index % listData.length]);
        },
        separatorBuilder: (context, index) {
          return const Divider(
            height: 1,
            color: Colors.black12,
          );
        },
        itemCount: listData.length * 100);
  }
}
